package com.eshop.we.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.eshop.we.area.entity.Area;
import com.eshop.we.business.AreaBusiness;
import com.eshop.we.business.SchoolBusiness;
import com.eshop.we.business.UserBusiness;
import com.eshop.we.result.Result;
import com.eshop.we.school.entity.School;
import com.eshop.we.user.entity.User;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;


@Api(value = "学校接口管理", description = "学校接口管理")
@RestController
@RequestMapping("/school")
public class SchoolController extends BaseController {

    @Autowired
    SchoolBusiness schoolBusiness;

    @Autowired
    UserBusiness userBusiness;

    @Autowired
    AreaBusiness areaBusiness;

    @ApiOperation(value = "模糊查询学校",notes = "模糊查询学校")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "proId", required = true, dataType = "Integer", paramType = "query", value = "省市县级别"),
            @ApiImplicitParam(name = "cityd", required = true, dataType = "Integer", paramType = "query", value = "父级id"),
            @ApiImplicitParam(name = "couId", required = true, dataType = "Integer", paramType = "query", value = "地区id"),
            @ApiImplicitParam(name = "name", required = true, dataType = "String", paramType = "query", value = "学校名称")
    })
    @GetMapping("/getschoollist")
    public Result getShoolList(@ApiParam(value = "省id", required = false)  @RequestParam(required = false) Integer proId,
                               @ApiParam(value = "市id", required = false)  @RequestParam(required = false) Integer cityId,
                               @ApiParam(value = "县id", required = false)  @RequestParam(required = false) Integer couId,
                               @ApiParam(value = "学校name", required = false)  @RequestParam(required = false) String name){
        EntityWrapper ew=new EntityWrapper<School>();
        if(proId!=null){ ew.eq("pro_id",proId);}
        if(cityId!=null){ew.eq("city_id",cityId); }
        if(couId!=null){ew.eq("cou_id",couId); }
        if(name!=null && !"".equals(name)){ew.like("name","%"+name+"%");};
        return Result.success(schoolBusiness.selectList(ew));
    }



    @ApiOperation(value = "新增用户认证信息",notes = "新增用户认证信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", required = true, dataType = "Integer", paramType = "query", value = "手机号"),
            @ApiImplicitParam(name = "name", required = true, dataType = "Integer", paramType = "query", value = "真实姓名"),
            @ApiImplicitParam(name = "proId", required = true, dataType = "Integer", paramType = "query", value = "省id"),
            @ApiImplicitParam(name = "cityId", required = true, dataType = "Integer", paramType = "query", value = "市id"),
            @ApiImplicitParam(name = "couId", required = true, dataType = "Integer", paramType = "query", value = "县id"),
            @ApiImplicitParam(name = "schoolId", required = true, dataType = "String", paramType = "query", value = "学校id")
    })
    @PostMapping("/insertuser")
    public Result insertSchool(@RequestParam(required = true) String mobile,@RequestParam(required = true)String name,Integer proId,Integer cityId,Integer couId,Integer schoolId, @ApiIgnore Integer userId){
        User user=new User();
        user.setId(userId);
        user.setMobile(mobile);
        user.setRealName(name);
        user.setProId(proId);
        user.setCityId(cityId);
        user.setCorpId(couId);
        user.setSchoolId(schoolId);
        return Result.success(userBusiness.updateById(user));
    }

    @GetMapping("/export")
    public void export(){
        File file = new File("C:\\Users\\LENOVO\\Documents\\WeChat Files\\ercuan0712\\Files\\全国初高中、大学学校信息\\highCollegeCollection\\highCollegeCollection");
        File[] children = file.listFiles();
        for (File f: children) {
            String fName = f.getName();
            String pName = fName.substring(0,fName.lastIndexOf("."));
            //查询省编码
            Area area = new Area();
            area.setShortName(pName);
            area.setLevelType(1);
            EntityWrapper ew=new EntityWrapper<Area>();
            ew.setEntity(area);
            Area are = areaBusiness.selectOne(ew);
            Integer proId = are.getAreaId();

            BufferedReader reader=null;
            String temp=null;
            List<School> schools = new ArrayList<>();
            try{
                reader=new BufferedReader(new FileReader(f));
                while((temp=reader.readLine())!=null){
                    if(temp != null){
                        String[] datas = temp.split(" ");
                        String schoolName = datas[1];
                        String code = datas[2];
                        String cityName = datas[3];
                        //查询省编码
                        Area areaCity = new Area();
                        areaCity.setName(cityName);
                        area.setLevelType(2);
                        EntityWrapper ewCity = new EntityWrapper<Area>();
                        ewCity.setEntity(area);
                        Area aCity = areaBusiness.selectOne(ewCity);
                        Integer cityId = aCity.getAreaId();
                        School s = new School();
                        s.setName(schoolName);
                        s.setCode(code);
                        s.setProId(proId);
                        s.setCityId(cityId);
                        s.setType(40);
                        s.setIsDel(0);
                       schools.add(s);
                    }
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
            finally{
                if(reader!=null){
                    try{
                        reader.close();
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
            schoolBusiness.insertBatch(schools);

        }

    }

    public static void main(String[] args) {

    }


}
